var userInfo;
var fid = 0;
var fbstatus ;
var accessToken ;
var imgArray;
// Load fb js
window.fbAsyncInit = function() {
  FB.init({
	appId		: appid, // App ID
	status		: true, // check login status
	cookie		: true, // enable cookies to allow the server to access the session
	xfbml		: true  // parse XFBML
  });
  // Additional initialization code here
  
  // listen for and handle auth.statusChange events
  FB.getLoginStatus(function(response) {
	 fbstatus = response.status;
	  if (response.status === 'connected') {
		// the user is logged in and has authenticated your
		// app, and response.authResponse supplies
		// the user's ID, a valid access token, a signed
		// request, and the time the access token 
		// and signed request each expire
		fid = response.authResponse.userID;
		accessToken = response.authResponse.accessToken;
		FB.api('/me', function(response) {
			femail = response.email;
			// Load image
		  
		});
	  } else if (response.status === 'not_authorized') {
		// the user is logged in to Facebook, 
		// but has not authenticated your app
	  } else {
		// the user isn't logged in to Facebook.
	  }
	 });

	 
		
	
};
// Load the SDK Asynchronously
(function(d){
   var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = "//connect.facebook.net/en_US/all.js";
   ref.parentNode.insertBefore(js, ref);
 }(document));
 
 
 

function lgfb(){
	if(fbstatus == 'connected')
	 {
		// notify
		FB.api('/me',function(resp){
				$.post(homeURL+'/users/connectFB?id='+resp.id,{token : accessToken , info : resp},function(rs){
					
					
					if(rs.error == false)
					{
						if(rs.post){
							
							window.setTimeout(function() {
								location.reload();
							}, 500);
						}else{
							location.reload();
						}
						
					}
				});
			});
	 }
	 else
	 {
		// console.log(fbstatus)
		// notify
		FB.login(function(response) {
		   if (response.authResponse) {
			 // redirect to fb create user
			accessToken = response.authResponse.accessToken;
			FB.api('/me',function(resp){
				
				$.post(homeURL+'/users/connectFB?id='+resp.id,{token : accessToken , info : resp},function(rs){
					
					
					if(rs.error == false)
					{
						if(rs.post){
							
							window.setTimeout(function() {
								location.reload();
							}, 500);
						}else{
							location.reload();
						}
						
					}
				});
			});
		   } 
		 },{scope: 'user_photos,user_videos,email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown'});
	 }
}


